package com.javacv.plus.extensions.image.steps;

import com.javacv.plus.core.processor.ProcessorContext;
import com.javacv.plus.core.processor.ProcessorStep;
import com.javacv.plus.extensions.image.ImageProcessor;
import com.javacv.plus.extensions.image.ProcessorResult;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Size;
import org.bytedeco.opencv.global.opencv_imgproc;

/**
 * 模糊效果步骤
 */
@Slf4j
public class BlurStep implements ProcessorStep<ImageProcessor, ProcessorResult> {

    private final int kernelSize;

    public BlurStep(int kernelSize) {
        if (kernelSize <= 0 || kernelSize % 2 == 0) {
            throw new IllegalArgumentException("核大小必须为正奇数");
        }
        this.kernelSize = kernelSize;
    }

    @Override
    public ProcessorResult execute(ImageProcessor processor, ProcessorContext context) throws Exception {
        Mat currentImage = context.getAttribute("currentImage");
        if (currentImage == null) {
            throw new IllegalStateException("当前图像为空");
        }

        log.debug("应用模糊效果，核大小: {}", kernelSize);

        Mat blurredImage = new Mat();
        Size kernelSizeObj = new Size(kernelSize, kernelSize);
        
        opencv_imgproc.GaussianBlur(currentImage, blurredImage, kernelSizeObj, 0);

        context.setAttribute("currentImage", blurredImage);
        context.setAttribute("processedImage", blurredImage);

        log.debug("模糊效果应用完成");
        return null;
    }

    @Override
    public String getStepName() {
        return "Blur";
    }

    @Override
    public int getPriority() {
        return 70;
    }
} 